package summerprojectFX.view;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.util.ArrayList;
import summerprojectFX.model.Song;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.event.EventType;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.input.*;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.scene.text.TextBuilder;
import javafx.stage.Stage;
import summerprojectFX.controller.MenuController;

public class FullScreenView extends Application{

	private ArrayList<Song> song;
	private int index;
	private Text currentSong;
        private ScrollPane sp;
        private int textSize = 15;
        private Stage primaryStage;
        private MainView view;
	
	public FullScreenView(ArrayList<Song> allOpenSongs, Song s, MainView view){
		song = allOpenSongs;
		int i = 0;
		for(Song temp: allOpenSongs){
			if(s.getTitle().equals(temp.getTitle())){
				index = i;
				break;
			}
			i++;
		}
                this.view = view;
		
	}
	@Override
	public void start(Stage primaryStage)  {
		this.primaryStage = primaryStage;
		
		primaryStage.setTitle("Song");

		// Gets Monitors resolution for the JFrame
		Toolkit toolkit = Toolkit.getDefaultToolkit();
		Dimension scrnsize = toolkit.getScreenSize();
		
                
		Group root = new Group();
		Scene scene = new Scene(root, scrnsize.width - 50,
				scrnsize.height - 50 );
		
		
		currentSong = TextBuilder.create()
				.fill(Color.BLACK)
				.font(Font.font("monospace", FontWeight.BOLD, textSize)).build();
		 sp = new ScrollPane();
		sp.setContent(currentSong);
                
		sp.setPrefHeight(scrnsize.getHeight()  - 50 );
		sp.setPrefWidth(scrnsize.getWidth() - 50);
		sp.setStyle("-fx-padding: 20 0 0 20;");
                                     
            
                                                                    
		currentSong.setText(song.get(index).getTitle() + "\n" + song.get(index).getLyrics());
		
               
                root.getChildren().add( sp);
		primaryStage.setScene(scene);
		primaryStage.show();
		
		sp.addEventFilter(KeyEvent.KEY_PRESSED, new EventHandler<KeyEvent>() {
	        @Override
	        public void handle(KeyEvent keyEvent) {
	        	if (keyEvent.getCode().equals(KeyCode.RIGHT) && index + 1 < song.size()) { 
	        		index++;
	        	    currentSong.setText(song.get(index).getTitle() + "\n" + song.get(index).getLyrics());
	        	}
	        	else if (keyEvent.getCode().equals(KeyCode.LEFT) && index - 1 > -1) { 
	        		index--;
	        	    currentSong.setText(song.get(index).getTitle() + "\n" + song.get(index).getLyrics());
	        	} else if(keyEvent.getCode().equals(KeyCode.DOWN)){
                            sp.setVvalue(sp.getVvalue() + .03);
                        }else if(keyEvent.getCode().equals(KeyCode.UP)){
                            sp.setVvalue(sp.getVvalue() - .03);
                        }else if(keyEvent.getCode().equals(KeyCode.Z) ){
                             textSize+=2;
                            currentSong.setStyle("-fx-font-size: " + textSize  + ";-fx-font-weight: bold;-fx-font-family: monospace;");
                            
                        }else if(keyEvent.getCode().equals(KeyCode.X) && textSize > 10){
                            textSize-=2;
                            currentSong.setStyle("-fx-font-size: " + textSize  + " ;-fx-font-weight: bold;-fx-font-family: monospace;");
                            
                        }
	        }
	    });
           if(view.getCheckedFull() == false){
                final Stage dialogStage = new Stage();
                                            dialogStage.setTitle("Set Key");
                                            Group root1 = new Group();
                                            Scene scene1 = new Scene(root1, 340,
                                    150 );
                                            Label key = new Label("Use ← and  →  on your keyboard to scroll through songs.\n" +
                                                    "Use ↑ and ↓ to scroll." +
                                                    "\nUse \"Z\" to zoom in and \"X\" to zoom out");
                                            
                                            Label choice = new Label("Dont show this message again");
                                            final CheckBox check = new CheckBox();
                                            VBox vb1 = new VBox();
                                            Button ok = new Button("Ok");
                                            ok.setOnAction(new EventHandler<ActionEvent>() {
                                                    public void handle(final ActionEvent event) {
                                                            {
                                                                if(check.isSelected()){
                                                                   view.setCheckedFull(true);
                                                                }
                                                              dialogStage.close();
                                                                    
                                                                
                                                            }
                                                    }
                                            });
                                            vb1.setAlignment(Pos.CENTER);
                                            HBox hb = new HBox();
                                            hb.getChildren().addAll(check, choice);
                                            vb1.getChildren().addAll(key, hb, ok );
                                            vb1.setSpacing(10);
                                            vb1.setStyle("-fx-padding: 20 0 0 10;");
                                           hb.setAlignment(Pos.CENTER);
                                            root1.getChildren().add(vb1);
                                       
                                            dialogStage.setScene(scene1);
                                            dialogStage.show();
           }
                
	}

  

}
